<?php
namespace app\admin\controller;

use app\admin\controller\BaseController;

/**
 * 后台首页
 * Class Index
 */
class IndexController extends BaseController {

	protected function _initialize() {
		parent::_initialize();
	}

	/**
	 * 首页
	 * @return mixed
	 */
	public function index() {
		$data['ytotal_vis']=db("visit")->whereTime("add_time","yesterday")->cache(30)->count();
		$data['total_vis']=db("visit")->cache(30)->count();
		$mems=db('member')->field("add_time,is_rz,is_pt,is_city,is_qy,rz_type")->cache(30)->select();
		$data['total_hy']=0;
		$data['total_qy']=0;
		$data['total_pt']=0;
		$data['total_ct']=0;
		$data['total_zc']=0;
		$data['fafang']=0;
		$data['total_ff']=0;
		foreach($mems as $v){
			if($v['is_rz']==1&&$v['rz_type']==1){
					$data['fafang']++;
					$data['total_ff']++;
			}
			if($v['is_rz']!=1){
				$data['total_zc']++;
			}elseif($v['is_rz']==1&&$v['is_city']==1){
				$data['total_ct']++;
			}elseif($v['is_rz']==1&&$v['is_pt']==1){
				$data['total_pt']++;
			}elseif($v['is_rz']==1&&$v['is_qy']==1){
				$data['total_qy']++;
			}else{
				$data['total_hy']++;
			}
		}
		$ymems=db('member')->whereTime('add_time','yesterday')->field("add_time,is_rz,is_pt,is_city,is_qy,rz_type")->cache(30)->select();
		$data['ytotal_hy']=0;
		$data['ytotal_qy']=0;
		$data['ytotal_pt']=0;
		$data['ytotal_ct']=0;
		$data['ytotal_zc']=0;
		foreach($ymems as $v){
			if($v['is_rz']!=1){
				$data['ytotal_zc']++;
			}elseif($v['is_rz']==1&&$v['is_city']==1){
				$data['ytotal_ct']++;
			}elseif($v['is_rz']==1&&$v['is_pt']==1){
				$data['ytotal_pt']++;
			}elseif($v['is_rz']==1&&$v['is_qy']==1){
				$data['ytotal_qy']++;
			}else{
				$data['ytotal_hy']++;
			}
		}
		
		//奖励金发放
		$data["y_jlj"]=db('hb_list')->whereTime('add_time','yesterday')->sum('balance');
		$data['total_jlj']=db('hb_list')->sum('balance');
		
		
		$y_yj=db('gdyj_record')->field("type,balance")->cache(30)->select();
		$data['ztyj']=0;
		$data['jtyj']=0;
		$data['ptyj']=0;
		$data['ctyj']=0;
		
		foreach($y_yj as $v){
			if($v['type']==6||$v['type']==10||$v['type']==14){
				$data['ztyj']+=$v['balance'];
			}elseif($v['type']==7||$v['type']==11||$v['type']==15){
				$data['jtyj']+=$v['balance'];
			}elseif($v['type']==8||$v['type']==9||$v['type']==12||$v['type']==16){
				$data['ptyj']+=$v['balance'];
			}elseif($v['type']==5||$v['type']==13|$v['type']==17||$v['type']==19){
				$data['ctyj']+=$v['balance'];
			}
		}
		
		
		$y_yj=db('gdyj_record')->whereTime('add_time','yesterday')->field("type,balance")->cache(30)->select();
		$data['y_ztyj']=0;
		$data['y_jtyj']=0;
		$data['y_ptyj']=0;
		$data['y_ctyj']=0;
		
		foreach($y_yj as $v){
			if($v['type']==6||$v['type']==10||$v['type']==14){
				$data['y_ztyj']+=$v['balance'];
			}elseif($v['type']==7||$v['type']==11||$v['type']==15){
				$data['y_jtyj']+=$v['balance'];
			}elseif($v['type']==8||$v['type']==9||$v['type']==12||$v['type']==16){
				$data['y_ptyj']+=$v['balance'];
			}elseif($v['type']==5||$v['type']==13|$v['type']==17||$v['type']==19){
				$data['y_ctyj']+=$v['balance'];
			}
		}
		
		
		
		$data['sj']=db('goods')->where("is_on_sale",1)->count();
		$data['xj']=db('goods')->where("is_on_sale",2)->count();
		$data['total_goods']=($data['sj']?$data['sj']:0)+($data['xj']?$data['xj']:0);
		
		//订单管理
		$wo['status']=['neq',5];
		$data['orders']=db('good_order')->where($wo)->cache(60)->count();
		$data['dfk']=db('good_order')->where("status",1)->count();
		$wo['status']=['in',[2,3,4]];
		$data['yfk']=db('good_order')->where($wo)->count();
		$data['dfh']=db('good_order')->where("status",2)->count();
		$data['dsh']=db('good_order')->where("status",3)->count();
		
		
		
		//财务管理
		$data['jlj']=db('hb_list')->cache(60)->sum('balance');
		$data['yj']=db('gdyj_record')->cache(60)->sum('balance');
		$data['tx']=db('member_tixian')->where("status",2)->sum('money');
		$data['total_cw']=($data['jlj']?$data['jlj']:0)+($data['yj']?$data['yj']:0)+($data['tx']?$data['tx']:0);		
		
		$this->assign('data',$data);
		return $this->fetch();
	}

	/**
	 * 清除缓存
	 */
	public function clear() {
		if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
			$this->success('清除缓存成功');
		} else {
			$this->error('清除缓存失败');
		}
	}

	/**
	 * 更新密码
	 * @param $id
	 * @param $group_id
	 */
	public function update() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			if (empty($data['old_pwd'])) {
				$this->error('请输入旧密码');
			}
			if (empty($data['login_pwd']) || empty($data['login_pwd2'])) {
				$this->error('请输入新密码');
			}
			if ($data['login_pwd'] != $data['login_pwd2']) {
				$this->error('二次密码输入不一致');
			}
			if (md5_salt($data['old_pwd']) != session('ADMIN.login_pwd')) {
				$this->error('旧密码错误');
			}
			$data2['login_pwd'] = md5_salt($data['login_pwd']);
			$data2['id'] = session('ADMIN.id');
			if (db('user')->update($data2) !== false) {
				$this->success('更新成功');
			} else {
				$this->error('更新失败');
			}
		} else {
			return $this->fetch('update', array('user' => session('ADMIN')));
		}
	}

}
